const { verifyJwtToken } = require('../util/server_jwt');

module.exports = async function checkJWTMiddleWare(req, res, next) {
    let token = req.headers.authorization;
    if (!token) {
        res.status(401).json({ "success": false, msg: "无效操作" });
        return ;
    } else {
        let result;
        try {
            result = await verifyJwtToken(token);
        } catch (err) {
            result = { login: false };
        }
        if (!result.login) {
            res.status(401).json({ "success": false, msg: "无效操作, 登录状态过期, 请重新登录" });
            return ;
        } else {
            req.jwtResult = result;
        }
    }
    next();
}